package vn.keho.module.dompeto.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bingkaimedia.merchantbeta.R;

import org.json.JSONArray;
import org.json.JSONObject;

import vn.keho.define.convert.JsonUtil;
import vn.keho.define.convert.StringUtil;
import vn.keho.module.dompeto.MerchantActivity;
import vn.keho.module.dompeto.model.QrCodeModel;
import vn.keho.module.dompeto.model.ServiceModel;
import vn.keho.module.dompeto.rest.DompetoDefine;
import vn.keho.module.dompeto.rest.DompetoParam;
import vn.keho.module.dompeto.rest.DompetoRequest;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 * Use the {link vn.keho.module.dompeto.fragment.LeftFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ServiceFragment extends Fragment {
    public static final int SERVICE_FRAGMENT_ID = 1;

    private Button btnSubmit;
    private TextView txtServiceTitle;
    private EditText txtAmount;

    private ServiceModel serviceModel;

    // TODO: Rename and change types and number of parameters
    public static ServiceFragment newInstance(ServiceModel serviceModel) {
        ServiceFragment fragment = new ServiceFragment();
        fragment.serviceModel = serviceModel;
        return fragment;
    }

    public ServiceFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_service, container, false);

        txtAmount = (EditText) view.findViewById(R.id.txtAmount);
        txtServiceTitle = (TextView) view.findViewById(R.id.txtServiceTitle);
        txtServiceTitle.setText(serviceModel.getServicetitle());

        btnSubmit = (Button) view.findViewById(R.id.btnSubmit);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!StringUtil.isBlank(txtAmount.getText().toString())) {
                    InputMethodManager mgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    mgr.hideSoftInputFromWindow(txtAmount.getWindowToken(), 0);
                    onSubmitRequestQrInfo();
                }
            }
        });
        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }


    public void onSubmitRequestQrInfo() {
        try {

            DompetoParam params = new DompetoParam(DompetoDefine.URL_MERCHANT_QR_CODE_REQUEST);
            params.setUsername(DompetoRequest.username);
            params.setMappsidonapps(DompetoRequest.mappsidonapps);
            params.setToken(DompetoRequest.token);
            params.setChannelid(DompetoRequest.channelid);
            params.setServiceid(serviceModel.getServiceid() + "");
            params.setAmount(txtAmount.getText().toString());
            new DompetoRequest(getActivity(), new DompetoRequest.AsyncCallbackRequestHandler() {
                @Override
                public void handlerRequestSuccess(Object data) {
                    try {
                        JSONArray jsonArray = new JSONArray(data.toString());
                        JSONObject jsonObject = jsonArray.getJSONObject(0);
                        QrCodeModel qrCodeModel = JsonUtil.generateJavaObject(jsonObject.toString(), QrCodeModel.class);
                        if (qrCodeModel.getRequeststatus()) {
                            System.out.println("data:" + JsonUtil.buildJsonString(qrCodeModel.getQrcodeinformation()));
                            MerchantActivity merchantActivity = (MerchantActivity) getActivity();
                            merchantActivity.onGenerateQrCode(qrCodeModel);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void handlerRequestError(String message) {
                    Log.i("Response", message);

                }
            }).executeStart(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
